package org.ihelpuoo.weboo.bean;

import java.util.Map;

import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.weboo.action.http.Action;
import org.ihelpuoo.weboo.exception.ActionInstanceException;
import org.ihelpuoo.weboo.exception.ActionNotFoundException;

/**
 * <b>Action class:</b>
 * <hr color=grey size=1>
 *
 *
 *
 * @author zozoh  created @ 2006
 *
 */
public class ActionBean
{
	public ActionBean()
	{
		super();
	}

	private String					name;
	private String					parent;
	private String					type;
	private String					comment;
	private Map<String, ParamBean>		params;
	private Map<String, PropertyBean>	properties;

	private Action					_action;
	private boolean					_isMerged;

	public boolean isMerged()
	{
		return _isMerged;
	}

	public void setMerged(boolean merged)
	{
		_isMerged = merged;
	}

	public boolean isAbstract()
	{
		return StringUtils.isBlank(type);
	}

	public String getParent()
	{
		return parent;
	}

	public void setParent(String parent)
	{
		this.parent = parent;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		this.type = StringUtils.trim(type);
		this._action = null;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = StringUtils.trim(name);
	}

	public String getComment()
	{
		return comment;
	}

	public void setComment(String comment)
	{
		this.comment = StringUtils.trim(comment);
	}

	public Map<String, ParamBean> getParams()
	{
		return params;
	}

	public void setParams(Map<String, ParamBean> paramConfigs)
	{
		this.params = paramConfigs;
	}

	public Map<String, PropertyBean> getProperties()
	{
		return properties;
	}

	public void setProperties(Map<String, PropertyBean> properties)
	{
		this.properties = properties;
	}

	public Action getAction() throws ActionNotFoundException, ActionInstanceException
	{
		if (isAbstract())
			throw new ActionInstanceException(name + " is abstrace action.");

		if (null == _action)
		{
			synchronized (this)
			{
				if (null == _action)
				{
					try
					{
						_action = (Action)Class.forName(getType()).newInstance();
					}
					catch (Exception e)
					{
						throw new ActionNotFoundException(e.getClass().getSimpleName() + ":"
								+ e.getMessage());
					}
				}
			}
		}
		return _action;
	}

}
